<?php
/**
 * 管理
 *
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0
 * @since 2011-06-03
 * @author cmd
 */

class adminApp extends app
{

	public function __contruct(){
		$this->_init();
		
		$this->login_need();
	}

	public function index(){
		go_to('/admin/flag');
	}

	public function flag(){
		$flagmod = $this->getModel('flag');
		$fl = $flagmod->get_flags();
		$data = $this->getData('post');

		if(empty($data)){
			if(empty($fl)){
				$this->inner_msg('没有获得数据');
				return array();
			}else{
				return array('fl'=> $fl);
			}
		}
		return array();

	}

	public function flagdelete(){
		$data = $this->getData('get');
		if(empty($data)){
			$this->inner_msg('什么也没发生，因为不知道删除谁');
			go_to('index');
			return array();
		}

		$flag_id = $data['flag_id'];
		$flag_id = intval($flag_id);
		$flagmod = $this->getModel('flag');
		$r = $flagmod->delete($flag_id);
		if($r>0){
			go_to('flag');
			exit;
		}else{
			echo 'fail';
			exit;
		}
	}

	public function flagupdate(){
		$data = $this->getData('post');
		if(empty($data)){
			$this->inner_msg('Post什么也没有获得');
			return array();
		}else{



			if($data['name'] == ''){
				return array('msg'=> '请输入名字');
			}
			if(empty($data['flag_user_edit'])){
				$data['flag_user_edit'] = 0;
			}
			if(empty($data['flag_flag_edit'])){
				$data['flag_flag_edit'] = 0;
			}
			if(empty($data['flag_group_create'])){
				$data['flag_group_create'] = 0;
			}
			if(empty($data['flag_knowledge_create'])){
				$data['flag_knowledge_create'] = 0;
			}
			if(empty($data['flag_knowledge_delete'])){
				$data['flag_knowledge_delete'] = 0;
			}
			if(empty($data['flag_work_create'])){
				$data['flag_work_create'] = 0;
			}
			if(empty($data['flag_todo_create'])){
				$data['flag_todo_create'] = 0;
			}


			$flagmod = $this->getModel('flag');
			$r = $flagmod->modify($data['flag_id'],$data['name'], $data['flag_user_edit'], $data['flag_flag_edit'], $data['flag_group_create'], $data['flag_knowledge_create'], $data['flag_knowledge_delete'], $data['flag_work_create'], $data['flag_todo_create']);
			if($r == 1){
				return array('msg'=> '修改成功');
				//return array();
			}else{
				return array('msg'=> '修改失败');
			}

		}
	}

	public function flagadd(){

		$data = $this->getData('post');
		if(empty($data))
		{
			return array();
		}
		if($data['name'] == ''){
			$this->inner_msg('请输入名字');
			return array();
		}
		if(empty($data['flag_user_edit'])){
			$data['flag_user_edit'] = 0;
		}
		if(empty($data['flag_flag_edit'])){
			$data['flag_flag_edit'] = 0;
		}
		if(empty($data['flag_group_create'])){
			$data['flag_group_create'] = 0;
		}
		if(empty($data['flag_knowledge_create'])){
			$data['flag_knowledge_create'] = 0;
		}
		if(empty($data['flag_knowledge_delete'])){
			$data['flag_knowledge_delete'] = 0;
		}
		if(empty($data['flag_work_create'])){
			$data['flag_work_create'] = 0;
		}
		if(empty($data['flag_todo_create'])){
			$data['flag_todo_create'] = 0;
		}
		$flagmod = $this->getModel('flag');
		$r = $flagmod->add($data['name'], $data['flag_user_edit'], $data['flag_flag_edit'], $data['flag_group_create'], $data['flag_knowledge_create'], $data['flag_knowledge_delete'], $data['flag_work_create'], $data['flag_todo_create']);
		if($r>0){
			//return array('msg' => '修改成功');
			go_to('/admin/flag','添加成功');
		}else{
			//return array('msg' => '修改失败');
			go_to('/admin/flag','添加失败');
		}
	}

	public function member(){

		//10 count every page.
		$pagesize = 100;

		$usermod = $this->getModel('user');

		
		$userlist = $usermod->get_users($pagesize, 1);
		
		$data = $this->getData('post');
		if(empty($data)){
			return array('member'=> $userlist);
		}

	}

	public function memberadd(){
		$data = $this->getData('post');
		$flagmod = $this->getModel('flag');
		$fs = $flagmod->get_flags();
		if(empty($data)){
			if($fs){
				return array('fs'=> $fs);
			}else{
				return array('fs'=> array(array('flag_id'=> 'defult', 'name'=> 'defult')));
			}
		}
		
		$usermod = & $this->getModel('user');
		$id = $usermod->add($data['email'], $data['password'], '未输入', $data['flag']);
		if($id > 0){
			go_to('member');
			exit;
		}
		else{
			echo 'fail';
			exit;
		}

	}

	public function memberedit(){
		$datapost = $this->getData('post');
		$dataget = $this->getData('get');
		
		$flagmod = $this->getModel('flag');
		$fs = $flagmod->get_flags();
		
		$usermod = $this->getModel('user');
		
		
		$u = $usermod->get_user_by_user_id($dataget['member_id']);
		
		
		if(empty($datapost)){
			if($fs){
				return array('fs'=> $fs, 'u'=> $u);
			}else{
				return array('fs'=> array(array('flag_id'=> 'defult', 'name'=> 'defult')));
			}
		}
		$usermod = & $this->getModel('user');	
		$usermod->update($datapost['user_id'],  $datapost['email'], $datapost['username'], $datapost['password'], $datapost['flag']);
		go_to('member');	
		
	}

	public function memberdelete(){
		$data = $this->getData('get');
		$member_id = $data['member_id'];
		$member_id = intval($member_id);
		$usermod = $this->getModel('user');
		$r = $usermod->delete($member_id);
		if($r>0){
			go_to('member');
			exit;
		}else{
			echo 'fail';
			exit;
		}
	}
}
?>